//学生管理模块
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../Include/student_pub.h"

//模块内部数据结构
Student students[MAX_STUDENT];
int student_count = 0;

void init_student_system() {
	student_count = 0;
	printf("学生管理系统初始化完成\n");
}

int add_student(const char* id, const char* name) {
	//检查学生数量是否已达上限
	if(student_count >= MAX_STUDENT) {
		printf("错误：学生数量已达上限(%d)\n",MAX_STUDENT);
		return -1;
	}

	//检查学号是否已存在
	if(find_student_by_id(id) != NULL) {
		printf("错误：学号 %s 已存在\n",id);
		return -1;
	}

	//检查学号是否为空
	if(strlen(id) == 0) {
		printf("错误，学号不能为空\n");
		return -1;
	}

	//添加到数组
	Student* new_student = &students[student_count];
	strncpy(new_student->id, id, MAX_ID_LEN - 1);
	strncpy(new_student->name, name, MAX_NAME_LEN - 1);
	new_student->course_count = 0;

	student_count++;
	printf("成功添加学生：%s %s\n",id,name);
	return 0;
}

int delete_student(const char* id) {
	for(int i = 0; i < student_count; i++) {
		if(strcmp(students[i].id, id) == 0) {
			//找到要删除的学生，将最后一个元素移到当前位置
			printf("删除学生：%s %s\n", students[i].id, students[i].name);

			if(i < student_count - 1) {
				students[i] = students[student_count - 1];
			}
			student_count--;
			return 0;
		}
	}
	printf("错误：未找到 %s 的学生\n", id);
	return -1;
}

int modify_student(const char* id, const char* new_name) {
	Student* student = find_student_by_id(id);
	if(student != NULL) {
		printf("修改学生信息：%s %s -> %s\n", student->id, student->name, new_name);
		strncpy(student->name, new_name, MAX_NAME_LEN - 1);
		return 0;
	}
	printf("错误：未找到学号 %s 的学生\n", id);
	return -1;
}
Student* find_student_by_id(const char* id) {
	for(int i = 0; i < student_count; i++) {
		if(strcmp(students[i].id, id) == 0) {
			return &students[i];
		}
	}
	return NULL;
}

Student* find_student_by_name(const char* name) {
	for (int i = 0; i < student_count; i++) {
		if(strcmp(students[i].name, name) == 0) {
			return &students[i];
		}
	}
	return NULL;
}

void list_all_students() {
	printf("\n=== 所有学生信息 ===\n");
	if(student_count == 0) {
		printf("暂无学生信息\n");
		return;
	}

	printf("总数：%d\n", student_count);
	for(int i = 0; i < student_count; i++) {
		printf("[%d] 学号：%s, 姓名：%s, 已选课程：%d,总学分：%d\n", i + 1, students[i].id, students[i].name, students[i].course_count, students[i].total_credits);
	}
}

int get_student_count() {
	return student_count;
}

int add_course_to_student(const char* student_id, const char* course_id, int credits) {
	Student* student = find_student_by_id(student_id);
	if(student == NULL) {
		printf("错误：未找到学生 %s\n",student_id);
		return -1;
	}

	if(student->course_count >= MAX_COURSES_PER_STUDENT) {
		printf("错误：学生 %s 选课数量已达上限(%d)\n", student_id, MAX_COURSES_PER_STUDENT);
		return -1;
	}

	//检查是否已选课程
	for(int i = 0; i < student->course_count; i++) {
		if(strcmp(student->selected_courses[i], course_id) == 0) {
			printf("错误：学生 %s 已选课程 %s\n", student_id, course_id);
			return -1;
		}
	}

	//添加到学生课程列表
	strncpy(student->selected_courses[student->course_count], course_id, MAX_ID_LEN - 1);
	student->course_count++;
	student->total_credits += credits;

	printf("学生 %s 成功选课：%s (学分：%d)\n",student_id, course_id, credits);
	return 0;
}

int remove_course_from_student(const char* student_id, const char* course_id) {
	Student* student = find_student_by_id(student_id);
	if(student == NULL) {
		printf("错误：未找到学生 %s\n", student_id);
		return -1;
	}

	for(int i = 0; i < student->course_count; i++) {
		if(strcmp(student->selected_courses[i], course_id) == 0) {
			//找到要退的课程，将最后一个课程移到当前位置
			printf("学生 %s 退课：%s\n",student_id, course_id);

			if(i < student->course_count - 1) {
				strncpy(student->selected_courses[i],
					student->selected_courses[student->course_count - 1],
					MAX_ID_LEN - 1);
			}
			student->course_count--;
			return 0;
		}
	}

	printf("错误：学生 %s 未选 %s\n", student_id, course_id);
	return -1;
}
